home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / os2 / pccts.zip / ANTLR.H < prev    next >
C/C++ Source or Header  |  1992-12-08  |  10KB  |  364 lines

  1. /*
  2.  * a n t l r . h
  3.  *
  4.  * Define all of the stack setup and manipulation of $i, #i variables.
  5.  *
  6.  *    Notes:
  7.  *        The type 'Attrib' must be defined before entry into this .h file.
  8.  *
  9.  * SOFTWARE RIGHTS
  10.  *
  11.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  12.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  13.  * company may do whatever they wish with source code distributed with
  14.  * PCCTS or the code generated by PCCTS, including the incorporation of
  15.  * PCCTS, or its output, into commerical software.
  16.  * 
  17.  * We encourage users to develop software with PCCTS.  However, we do ask
  18.  * that credit is given to us for developing PCCTS.  By "credit",
  19.  * we mean that if you incorporate our source code into one of your
  20.  * programs (commercial product, research project, or otherwise) that you
  21.  * acknowledge this fact somewhere in the documentation, research report,
  22.  * etc...  If you like PCCTS and have developed a nice tool with the
  23.  * output, please mention that you developed it using PCCTS.  In
  24.  * addition, we ask that this header remain intact in our source code.
  25.  * As long as these guidelines are kept, we expect to continue enhancing
  26.  * this system and expect to make other tools available as they are
  27.  * completed.
  28.  *
  29.  * ANTLR 1.06
  30.  * Terence Parr
  31.  * Purdue University
  32.  * 1989-1992
  33.  */
  34.  
  35. /* can make this a power of 2 for more efficient lookup */
  36. #ifndef ZZLEXBUFSIZE
  37. #define ZZLEXBUFSIZE    2000
  38. #endif
  39.  
  40. #define zzOvfChk                                                        \
  41.             if ( zzasp <= 0 )                                           \
  42.             {                                                           \
  43.                 fprintf(stderr, zzStackOvfMsg, __FILE__, __LINE__);        \
  44.                 exit(-1);                                               \
  45.             }
  46.  
  47. #ifndef ZZA_STACKSIZE
  48. #define ZZA_STACKSIZE    400
  49. #endif
  50. #ifndef ZZAST_STACKSIZE
  51. #define ZZAST_STACKSIZE    400
  52. #endif
  53.  
  54. #ifdef LL_K
  55. #define LOOKAHEAD                                                \
  56.     int zztokenLA[LL_K];                                        \
  57.     char zztextLA[LL_K][ZZLEXBUFSIZE];                            \
  58.     int zzlap = 0, zzlabase=0; /* labase only used for DEMAND_LOOK */
  59. #else
  60. #define LOOKAHEAD                                                \
  61.     int zztoken;
  62. #endif
  63.  
  64.  
  65. #ifndef zzcr_ast
  66. #define zzcr_ast(ast,attr,tok,text)
  67. #endif
  68.  
  69. #ifdef DEMAND_LOOK
  70. #define DemandLookData    int zzdirty=1;
  71. #else
  72. #define DemandLookData
  73. #endif
  74.  
  75. /* The zzStackOvfMsg error message is repeated often and therefore only one
  76.  * copy is made to save space.
  77.  */
  78. #ifdef LL_K
  79.  
  80. #ifdef __STDC__
  81. #define ANTLR_INFO                                                \
  82.     Attrib zzempty_attr() {static Attrib a; return a;}            \
  83.     Attrib zzconstr_attr(int _tok, char *_text)\
  84.         {Attrib a; zzcr_attr((&a),_tok,_text); return a;}        \
  85.     int zzasp=ZZA_STACKSIZE;                                    \
  86.     char zzStackOvfMsg[]="fatal: attrib/AST stack overflow %s(%d)!\n"; \
  87.     Attrib zzaStack[ZZA_STACKSIZE];                                \
  88.     int TCount=0, TComp=0; DemandLookData
  89. #else
  90. #define ANTLR_INFO                                                \
  91.     Attrib zzempty_attr() {static Attrib a; return a;}            \
  92.     Attrib zzconstr_attr(_tok, _text) int _tok; char *_text;\
  93.         {Attrib a; zzcr_attr((&a),_tok,_text); return a;}        \
  94.     int zzasp=ZZA_STACKSIZE;                                    \
  95.     char zzStackOvfMsg[]="fatal: attrib/AST stack overflow %s(%d)!\n"; \
  96.     Attrib zzaStack[ZZA_STACKSIZE];                                \
  97.     int TCount=0, TComp=0; DemandLookData
  98. #endif
  99.  
  100. #else
  101.  
  102. #ifdef __STDC__
  103. #define ANTLR_INFO                                                \
  104.     Attrib zzempty_attr() {static Attrib a; return a;}            \
  105.     Attrib zzconstr_attr(int _tok, char *_text)\
  106.         {Attrib a; zzcr_attr((&a),_tok,_text); return a;}        \
  107.     int zzasp=ZZA_STACKSIZE;                                    \
  108.     char zzStackOvfMsg[]="fatal: attrib/AST stack overflow %s(%d)!\n"; \
  109.     Attrib zzaStack[ZZA_STACKSIZE];                                \
  110.     int TCount=0, TComp=0; DemandLookData
  111. #else
  112. #define ANTLR_INFO                                                \
  113.     Attrib zzempty_attr() {static Attrib a; return a;}            \
  114.     Attrib zzconstr_attr(_tok, _text) int _tok; char *_text;\
  115.         {Attrib a; zzcr_attr((&a),_tok,_text); return a;}        \
  116.     int zzasp=ZZA_STACKSIZE;                                    \
  117.     char zzStackOvfMsg[]="fatal: attrib/AST stack overflow %s(%d)!\n"; \
  118.     Attrib zzaStack[ZZA_STACKSIZE];                                \
  119.     int TCount=0, TComp=0; DemandLookData
  120. #endif
  121.     
  122. #endif /* LL_k */
  123.  
  124. #ifdef LL_K
  125.  
  126. #ifdef DEMAND_LOOK
  127. #define zzPrimeLookAhead  {zzdirty=LL_K; zzlap = zzlabase = 0;}
  128. #else
  129. #define zzPrimeLookAhead  {int _i; for(_i=1;_i<=LL_K; _i++)        \
  130.                                         {zzCONSUME;} zzlap = 0;}
  131. #endif
  132. #else
  133.  
  134. #ifdef DEMAND_LOOK
  135. #define zzPrimeLookAhead  zzdirty=1
  136. #else
  137. #define zzPrimeLookAhead  zzgettok()
  138. #endif
  139.  
  140. #endif    /* LL_K */
  141.  
  142. #ifdef LL_K
  143. #define zzenterANTLRf(f)                            \
  144.         zzlextext = &(zztextLA[0][0]); zzrdfunc( f ); zzPrimeLookAhead;
  145. #define zzenterANTLR(f)                            \
  146.         zzlextext = &(zztextLA[0][0]); zzrdstream( f ); zzPrimeLookAhead;
  147. #else
  148. #define zzenterANTLRf(f)                            \
  149.         {static char zztoktext[ZZLEXBUFSIZE];    \
  150.         zzlextext = zztoktext; zzrdfunc( f ); zzPrimeLookAhead;}
  151. #define zzenterANTLR(f)                            \
  152.         {static char zztoktext[ZZLEXBUFSIZE];    \
  153.         zzlextext = zztoktext; zzrdstream( f ); zzPrimeLookAhead;}
  154. #endif
  155.  
  156. #define ANTLR(st, f)    zzbufsize = ZZLEXBUFSIZE;    \
  157.                         zzenterANTLR(f);            \
  158.                         st; ++zzasp;
  159. /*
  160. #define ANTLR(st, f)    zzbufsize = ZZLEXBUFSIZE;    \
  161.                         zzenterANTLR(f);            \
  162.                         st; ++zzasp;                \
  163.                         fprintf(stderr, "TCount %d, TComp %d; Ratio %f\n",    \
  164.                                         TCount, TComp,                        \
  165.                                         ((float)TComp)/TCount);
  166. */
  167. #define ANTLRf(st, f)    zzbufsize = ZZLEXBUFSIZE;    \
  168.                         zzenterANTLRf(f);            \
  169.                         st; ++zzasp;
  170.  
  171. /*#define ANTLRf(st, f)    zzbufsize = ZZLEXBUFSIZE;    \
  172.                         zzenterANTLRf(f);            \
  173.                         st; ++zzasp;                \
  174.                         fprintf(stderr, "TCount %d, TComp %d; Ratio %f\n",    \
  175.                                         TCount, TComp,                        \
  176.                                         ((float)TComp)/TCount);
  177. */
  178. #ifdef LL_K
  179. #define zztext        (&(zztextLA[zzlap][0]))
  180. #else
  181. #define zztext        zzlextext
  182. #endif
  183.  
  184.  
  185.                     /* A r g u m e n t  A c c e s s */
  186.  
  187. #define zzaCur            (zzaStack[zzasp])
  188. #define zzaRet            (*zzaRetPtr)
  189. #define zzaArg(v,n)        zzaStack[v-n]
  190. #define zzMakeAttr        {zzOvfChk; --zzasp; zzcr_attr(&(zzaStack[zzasp]),LA(1),LATEXT(1));}
  191. #ifdef zzdef0
  192. #define zzMake0            {zzOvfChk; --zzasp; zzdef0(&(zzaStack[zzasp]));}
  193. #else
  194. #define zzMake0            {zzOvfChk; --zzasp;}
  195. #endif
  196. #define zzaPush(_v)        {zzOvfChk; zzaStack[--zzasp] = _v;}
  197. #ifndef zzd_attr
  198. #define zzREL(t)        zzasp=(t);        /* Restore state of stack */
  199. #else
  200. #define zzREL(t)        for (; zzasp<(t); zzasp++)                \
  201.                         { zzd_attr(&(zzaStack[zzasp])); }
  202. #endif
  203.  
  204. #ifdef DEMAND_LOOK
  205.  
  206. #ifdef LL_K
  207. #define zzmatch(_t)                            \
  208.             {                                \
  209.             if ( zzdirty==LL_K ) {            \
  210.                 zzCONSUME;                    \
  211.             }                                \
  212.             if ( LA(1)!=_t ) {        \
  213.                 zzBadText=zzMissText=LATEXT(1);    \
  214.                 zzMissTok=_t; zzBadTok=LA(1); \
  215.                 zzMissSet=NULL;                \
  216.                 goto fail;                    \
  217.             }                                \
  218.             zzMakeAttr                        \
  219.             zzdirty++;                        \
  220.             zzlabase++;                        \
  221.             }
  222. #else
  223. #define zzmatch(_t)                            \
  224.             {                                \
  225.             if ( zzdirty ) {zzCONSUME;}        \
  226.             if ( LA(1)!=_t ) {                \
  227.                 zzBadText=zzMissText=LATEXT(1);    \
  228.                 zzMissTok=_t; zzBadTok=LA(1); \
  229.                 zzMissSet=NULL;                \
  230.                 goto fail;                    \
  231.             }                                \
  232.             zzdirty = 1;                    \
  233.             zzMakeAttr                        \
  234.             }
  235. #endif
  236.  
  237. #else
  238.  
  239. #define zzmatch(_t)        \
  240.             {                                \
  241.             if ( LA(1)!=_t ) {                \
  242.                 zzBadText=zzMissText=LATEXT(1);    \
  243.                 zzMissTok=_t; zzBadTok=LA(1); \
  244.                 zzMissSet=NULL;                \
  245.                 goto fail;                    \
  246.             }                                \
  247.             zzMakeAttr                        \
  248.             }
  249.  
  250. #endif /* DEMAND_LOOK */
  251.  
  252. #ifdef GENAST
  253. #define zzRULE        Attrib *zzaRetPtr = &(zzaStack[zzasp-1]);    \
  254.                     unsigned *zzMissSet; int zzMissTok=0;        \
  255.                     unsigned zzBadTok; char *zzBadText="";        \
  256.                     int zzErrk=1;                                \
  257.                     char *zzMissText; zzASTVars
  258. #else
  259. #define zzRULE        Attrib *zzaRetPtr = &(zzaStack[zzasp-1]);    \
  260.                     unsigned zzBadTok; char *zzBadText="";        \
  261.                     int zzErrk=1;                                \
  262.                     unsigned *zzMissSet; int zzMissTok=0; char *zzMissText
  263. #endif
  264.  
  265. #ifdef GENAST
  266. #define zzBLOCK(i)    int i = zzasp - 1; int zztsp = zzast_sp
  267. #define zzEXIT(i)    zzREL(i); zzastREL; zzastPush(*_root);
  268. #define zzLOOP(i)    zzREL(i); zzastREL
  269. #else
  270. #define zzBLOCK(i)    int i = zzasp - 1
  271. #define zzEXIT(i)    zzREL(i)
  272. #define zzLOOP(i)    zzREL(i)
  273. #endif
  274.  
  275. #ifdef LL_K
  276.  
  277. #ifdef DEMAND_LOOK
  278. #define LOOK(_k)    {int i,stop=_k-(LL_K-zzdirty); for (i=1; i<=stop; i++)    \
  279.                     zzCONSUME;}
  280. #define zzCONSUME    {zzgettok(); zzdirty--;                            \
  281.                     zzlap = (zzlap+1)&(LL_K-1);                        \
  282.                     zzlextext = &(zztextLA[zzlap][0]);}
  283. #else
  284. #define zzCONSUME    {zzgettok();                                     \
  285.                     zzlap = (zzlap+1)&(LL_K-1);                        \
  286.                     zzlextext = &(zztextLA[zzlap][0]);}
  287. #endif
  288.  
  289. #else
  290.  
  291. #ifdef DEMAND_LOOK
  292. #define LOOK(_k)    if ( zzdirty) zzCONSUME;
  293. #define zzCONSUME    zzgettok(); zzdirty=0;
  294. #else
  295. #define zzCONSUME    zzgettok();
  296.  
  297. #endif /* LL_K */
  298.  
  299. #endif /* LL_K */
  300.  
  301. #ifdef LL_K
  302. #define NLA            zztokenLA[zzlap&(LL_K-1)]    /* --> next LA */
  303. #define NLATEXT        zztextLA[zzlap&(LL_K-1)]    /* --> next text of LA */
  304. #ifdef DEMAND_LOOK
  305. #define LA(i)       zztokenLA[(zzlabase+(i)-1)&(LL_K-1)]
  306. #define LATEXT(i)   (&(zztextLA[(zzlabase+(i)-1)&(LL_K-1)][0]))
  307. #else
  308. #define LA(i)       zztokenLA[(zzlap+(i)-1)&(LL_K-1)]
  309. #define LATEXT(i)   (&(zztextLA[(zzlap+(i)-1)&(LL_K-1)][0]))
  310. #endif
  311. #else
  312. #define NLA            zztoken
  313. #define NLATEXT        zztext
  314. #define LA(i)       zztoken
  315. #define LATEXT(i)   zztext
  316. #endif
  317.  
  318.            /* F u n c t i o n  T r a c i n g */
  319.  
  320. #ifndef zzTRACEIN
  321. #define zzTRACEIN(r)    fprintf(stderr, "enter rule \"%s\"\n", r);
  322. #endif
  323. #ifndef zzTRACEOUT
  324. #define zzTRACEOUT(r)    fprintf(stderr, "exit rule \"%s\"\n", r);
  325. #endif
  326.  
  327.                 /* E x t e r n  D e f s */
  328.  
  329. #ifdef __STDC__
  330. extern Attrib zzempty_attr();
  331. extern Attrib zzconstr_attr(int, char *);
  332. extern void zzsyn(char *, unsigned, char *, unsigned *, int, int, char *);
  333. extern int zzset_el(unsigned , unsigned *);
  334. extern int zzset_deg(unsigned *);
  335. extern void zzedecode(unsigned *);
  336. extern void zzFAIL(int k, ...);
  337. extern void zzresynch(unsigned *,unsigned);
  338. #else
  339. extern Attrib zzempty_attr();
  340. extern Attrib zzconstr_attr();
  341. extern void zzsyn();
  342. extern int zzset_el();
  343. extern int zzset_deg();
  344. extern void zzedecode();
  345. extern void zzFAIL();
  346. extern void zzresynch();
  347. #endif
  348.  
  349. extern int TCount, TComp;
  350. extern char zzStackOvfMsg[];
  351. extern char *zztokens[];
  352. extern int zzasp;
  353. extern int zzbufsize;
  354. extern Attrib zzaStack[];
  355. #ifdef LL_K
  356. extern int zztokenLA[];
  357. extern char zztextLA[][ZZLEXBUFSIZE];
  358. extern int zzlap;
  359. extern int zzlabase;
  360. #else
  361. extern int zztoken;
  362. #endif
  363. extern int zzdirty;
  364.